package com.nl.controller.web.controller;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.apache.ibatis.javassist.expr.NewArray;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.nl.controller.common.model.Response;
import com.nl.controller.common.util.ResponseUtil;
import com.nl.controller.web.bean.SmsAllStation;
import com.nl.controller.web.bean.SmsMgrTemplateInfo;
import com.nl.controller.web.bean.SmsUsers;
import com.nl.controller.web.bean.XuYiStation;
import com.nl.controller.web.bean.sendLimit.QxjSendScope;
import com.nl.controller.web.service.SmsAreaService;

import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@RequestMapping("/smsArea")
@RestController
public class SmsAreaController {
    @Autowired
    private SmsAreaService smsAreaService;
    
    /**
     * 
     * @Description:  
     * @author liurr 
     * @date 2019年3月13日 下午5:28:55  
     * @param request
     * @param session
     * @param resultStation
     * @param regionId
     * @param mgrId
     * @param cityId
     * @param regionName
     * @return
     * @throws IOException
     */
    @RequestMapping(value = "/mapSave", method = RequestMethod.POST)
    public Response mapSave(HttpSession session,
            @RequestParam(value = "cityId", required = true) String cityId,
            @RequestParam(value = "mgrId", required = true) String mgrId,
            @RequestParam(value = "resultRegionType", required = true) String resultRegionType,
            @RequestParam(value = "regionId", required = true) String regionId,
            @RequestParam(value = "resultRegionValue", required = true) String resultRegionValue
//            , @RequestParam(value = "resultStation", required = true) String resultStation
    ) throws IOException{
//        JSONArray jsonArray=JSONArray.parseArray(resultStation);
        SmsUsers user = (SmsUsers) session.getAttribute("user");
        String  msisdn = user.getMsisdn();
        log.info(String.format("类：%s；方法：%s；参数：regionId:%s","SmsAreaController","mapSave",regionId));
        List<Object> result = smsAreaService.mapSave(cityId, mgrId, resultRegionType, resultRegionValue,msisdn,regionId);
        log.debug("画区域添加到基站信息表返回数据：{}", result);
        return ResponseUtil.sucess(result);
    }
    /**
     * 气象局圈划范围数据保存
     *	@author ll
     * @date 2019年11月18日 下午3:28:55
     * @param scopeId 范围编码
     * @param scopeType 圈画类型
     * @param longAndLats 经纬度合集
     * @return
     * @throws IOException
     */
    @RequestMapping(value = "/qxjScopeSave", method = RequestMethod.POST)
    public Response qxjScopeSave(
    		@RequestParam(value = "scopeId", required = true) String scopeId,
    		@RequestParam(value = "cityId", required = true) String cityId,
            @RequestParam(value = "scopeType", required = true) String scopeType,
            @RequestParam(value = "longAndLats") String longAndLats) throws IOException{
    	int  result = smsAreaService.qxjScopeSave(scopeId, cityId,scopeType, longAndLats);
        //String  result = smsAreaService.qxjScopeSave(scopeType, longAndLats);
        log.debug("画区域添加到气象局圈划范围表返回数据：{}", result);
        return ResponseUtil.sucess(result);
    }
    
    /**
     * 气象局圈划范围后同步数据接口
     * @date 2019年12月2日 下午3:05:55  
     * @author ll
     * @param request 请求报文
     * @return result 返回报文
     */
    @RequestMapping(value = "/qxjSynQry", method = RequestMethod.POST)
    @ResponseBody
    public JSONObject qxjSynQry( @RequestBody JSONObject request) {
    	String scopeId="";
    	String timeStamp="";
    	Long between = null;
    	try {
    		//JSONObject jsonObject = JSONObject.parseObject(body); //将body转化为相应的JSONObject对象
    		if(request.getString("scopeId")!=""&&request.getString("timeStamp")!=""){
    			scopeId=request.getString("scopeId");
    			timeStamp=request.getString("timeStamp");
    			log.info("气象局查询圈划范围参数：{}",scopeId+","+timeStamp);
    		}else{
    			log.info("查询圈划范围请求参数缺失：{}",scopeId+","+timeStamp);
    		}
    		Date date = new SimpleDateFormat("yyyyMMddHHmmss").parse(timeStamp);
    		Long now=new Date().getTime();
    		Long  time=date.getTime();
    		if(now>=time){
    			between=(now-time)/1000;
    		}else{
    			log.info("时间戳参数不符合规范：{}",timeStamp);
    		}
    		log.info("=================between"+between+"秒");
    	} catch (ParseException e) {
    		e.printStackTrace();
    	}
    	QxjSendScope result = smsAreaService.qxjScopeQry(scopeId);
    	log.info("气象局圈划范围查询返回数据：{}", result);
    	JSONObject response=new JSONObject();
    	Map<String,Object> map = new HashMap<String,Object>();
    	if(result!=null&&between<=180){
    		map.put("resultCode", "0");
    		map.put("resultDesc", "成功");
    		map.put("scopeId", result.getScopeId());
    		map.put("scopeType", result.getScopeType());
    		map.put("longAndLats", result.getLongAndLats());
    		
    	}else if(between>180){
    		map.put("resultCode", "-1");
    		map.put("resultDesc", "请求超时");
    	}else if(result==null){
    		map.put("resultCode", "-1");
    		map.put("resultDesc", "未查询到数据");
    	}
    	response.putAll(map);
    	return response;
    }
    
    /**
     * 气象局圈划范围数据查询
     * @date 2019年11月118日 下午3:37:55  
     * @author ll
     * @param scopeId 范围编码
     * @return
     * @throws IOException
     */
    @RequestMapping(value = "/qxjScopeQry", method = RequestMethod.POST)
    public Response qxjScopeQry(
           @RequestParam(value = "scopeId", required = true) String scopeId) throws IOException{
    	QxjSendScope result = smsAreaService.qxjScopeQry(scopeId);
        log.debug("气象局圈划范围查询返回数据：{}", result);
        return ResponseUtil.sucess(result);
    }
    
    @RequestMapping(value = "/countySave", method = RequestMethod.POST)
    public Response countySave(HttpSession session,
            @RequestParam(value = "countyId", required = true) String countyId,
            @RequestParam(value = "mgrId", required = true) String mgrId,
            @RequestParam(value = "cityId", required = true) String cityId,
            @RequestParam(value = "stationIds", required = true) String stationIds) throws IOException{
        List<Object> result = smsAreaService.countySave(cityId, mgrId,countyId,stationIds);
        log.debug("选区县添加到基站信息表返回数据：{}", result);
        return ResponseUtil.sucess(result);
    }
    
    @PostMapping("findAllStation")
    public List<SmsAllStation> findAllStation(
            @RequestParam(value = "cityId") String cityId,@RequestParam(value = "countyId") String countyId,
            @RequestParam(value = "longitudeMin") String longitudeMin,@RequestParam(value = "longitudeMax") String longitudeMax,
            @RequestParam(value = "latitudeMin") String latitudeMin,@RequestParam(value = "latitudeMax") String latitudeMax,
            @RequestParam Map<String, Object> paramMap) {
        List<SmsAllStation> list =new ArrayList<SmsAllStation>();
        list = smsAreaService.findAllStation(cityId, countyId, longitudeMin, longitudeMax, latitudeMin, latitudeMax);
        log.info(" 查询基站信息：{}", paramMap.toString());
        log.debug("查询基站信息返回参数：{}", list.toString());
        return list;
    }
    
    @PostMapping("findAllStationByCounty")
    public List<SmsAllStation> findAllStationByCounty(
            @RequestParam(value = "cityId") String cityId,@RequestParam(value = "countyId") String countyId,
            @RequestParam Map<String, Object> paramMap) {
        List<SmsAllStation> list =new ArrayList<SmsAllStation>();
//        list = smsAreaService.findAllStationByCounty(cityId, countyId);
        log.info(" 查询基站信息：{}", paramMap.toString());
        log.debug("查询基站信息返回参数：{}", list.toString());
        return list;
    }
    
    /**
     * desc:查询热力图显示极限等级
     * @author Administrator
     * @Date 2019年8月8日 下午5:30:58
     * @return
     */
    @PostMapping("findHeatMapLevel")
    public Integer findHeatMapLevel () {
        int level = smsAreaService.findHeatMapLevel();
        log.debug("查询热力图显示极限等级返回参数：{}", level);
        return level;
    }
    /**
     * desc:查询盱眙基站信息
     * @author zhangheng
     * @date 2020年1月16日 下午3:39:21
     * @return 
     * Response
     */
    @RequestMapping("findXuYiStations") 
    public Response findXuYiStations(){
    	return ResponseUtil.sucess( smsAreaService.findXuYiStations());
    }
    
    @RequestMapping("findXuYiSubStations") 
    public Response findXuYiSubStations(@RequestParam(value = "pathName") String pathName,@RequestParam(value = "subStationName") String subStationName){
    	return ResponseUtil.sucess( smsAreaService.findXuYiSubStations(pathName,subStationName));
    }
    
    
    @RequestMapping("findXuYiSubStationsList") 
    public Response findXuYiSubStationsList(
    		@RequestParam(value = "page", required = true) int page,
            @RequestParam(value = "limit", required = true) int limit,
    		@RequestParam(value = "pathName",required=false) String pathName,
    		@RequestParam(value = "subStationName",required=false) String subStationName,
    		@RequestParam(value = "subSeq",required=false) String subSeq){
    		PageInfo<XuYiStation> pageInfo = smsAreaService.findXuYiSubStations3(page,limit,pathName,subStationName);
    	    return ResponseUtil.sucess(pageInfo.getList(), pageInfo.getTotal());
    }
    
    @RequestMapping("findPathName") 
    public Response findPathName(){
    	return ResponseUtil.sucess( smsAreaService.findPathName());
    }
    
    @RequestMapping("findRegionName") 
    public Response findRegionName(String pathName){
    	return ResponseUtil.sucess( smsAreaService.findRegionName(pathName));
    }
    
    @PostMapping("findRegionNameLike")
    public Response<String> selectMgrNameLike(@RequestParam(value = "pathName", required = false) String pathName,
            @RequestParam(value = "regionName", required = false) String regionName
    		) {
        	List<String> list = new ArrayList<String>();
        	list = smsAreaService.findRegionNameLike(pathName, regionName);
        log.info("点位名称联想参数：{}", pathName);
        
        log.debug("点位名称联想返回数据：{}", list.size());
        return ResponseUtil.sucess(list);
    }    
    
    @PostMapping("findRegionNameLike2")
    public Response selectMgrNameLike2(
            @RequestParam(value = "regionNameText", required = false) String regionNameText,
            @RequestParam(value = "alreadyRelate", required = false) String alreadyRelate
    		) {
        	List<XuYiStation> list = new ArrayList<XuYiStation>();
        	PageInfo<XuYiStation> pageInfo  = smsAreaService.findRegionNameLike2(1,100000,regionNameText,alreadyRelate);
        log.info("点位名称联想参数：{}", regionNameText,alreadyRelate);
        
        log.debug("点位名称联想返回数据：{}", list.size());
        return ResponseUtil.sucess(pageInfo.getList(), pageInfo.getTotal());
    }    
    
    @PostMapping("findlnglatNames")
    public List<XuYiStation> findlnglatNames(@RequestParam(value = "gps", required = false) String gps) throws UnsupportedEncodingException {
    	log.info("-------"+gps);
    	List<XuYiStation> lnglatNames = smsAreaService.BdToGPS(gps);
        return lnglatNames;
    }    
    
    
    
}
